<template>
    <!-- component/select_sku/select_sku.wxml -->
    <view class="my-dialog" v-if="flag">
        <view class="my-mask"></view>
        <view class="my-container">
            <view class="my-context">
                <view class="v-center between titleBar">
                    <image @tap="hide" class="close" src="/static/file/images/close.png"></image>
                    <view class="proContent v-center">
                        <view class="pro-img">
                            <image :src="sku.Fimg"></image>
                        </view>
                        <view class="proInfo">
                            <view class="name">{{ sku.Fsummary }}</view>
                            <view class="describe">{{ Fattr }}</view>
                            <view class="pro_price">￥{{ sku.Fsaleprice }}</view>
                        </view>
                    </view>
                </view>
                <view class="sku_content">
                    <!-- 购买数量 -->
                    <view class="numBar v-center between">
                        <text>数量：</text>
                        <view class="v-center end buynum">
                            <text @tap="changeNum" data-type="sub" class="changeIcon">-</text>
                            <input @blur="inputNum" type="number" pattern="\d*" class="changeNum" :value="Fnum" />
                            <text @tap="changeNum" data-type="add" class="changeIcon">+</text>
                        </view>
                    </view>
                </view>
                <view
                    class="btnBar algin"
                    @tap="parseEventDynamicCode($event, (Fnum > 0) & (Fstocknum > 0) ? 'confirm' : '')"
                    :style="'background:' + ((Fnum > 0) & (Fstocknum > 0) ? color : '#aaa')"
                >
                    {{ (null == sku) | (Fstocknum > 0) ? '确定' : '库存不足' }}
                </view>
            </view>
        </view>
    </view>
</template>

<script>
// component/select_sku/select_sku.js 
var util = require('../../utils/util.js');
var api = require('../../utils/prom.js');
export default {
    data() {
        return {
            flag: false,
            color: app.globalData.color,
            sku: null,
            Fnum: 0,
            Fstocknum: 0,
            Fdesc: '',
            Fopt: 0,
            Fattr: ''
        };
    },
    /**
     * 组件的属性列表
     */
    props: {},
    /**
     * 组件的方法列表
     */
    methods: {
        // 活动SKu
        show(sku, type, Fattr, Fopt) {
            //请求可购数量
            var Fstocknum = Number(sku.Ftotal) - Number(sku.Fsalenum);
            var Fdesc = '';
            if (sku.Flimitnum > 0) {
                Fdesc += '活动每人限购:' + sku.Flimitnum + '件';
            }
            if (type != 6) {
                Fdesc += ' 当前可售:' + Fstocknum + '件';
            }
			this.sku = sku;
			this.flag = true;
			this.Fstocknum = Fstocknum;
			this.Fdesc = Fdesc;
			this.Fopt = Fopt;
			this.Fattr = Fattr;
            
        },
        //加或者减少数量
        changeNum: function (e) {
            var that = this;
            var type = e.currentTarget.dataset.type;
            var num = parseInt(that.Fnum);
            if (type == 'add') {
				that.Fnum =  num + 1;
               
            } else {
                if (num == 0) {
                    return;
                }
                if (num - 1 < 0) {
					that.Fnum =  0;
                    
                } else {
					that.Fnum =  num - 1;                   
                }
            }
        },
        //输入数量
        inputNum: function (e) {
            var that = this;
            var num = e.detail.value;
            if (num < 0) {
               that.Fnum =  0;
            } else {
                that.Fnum =  num;
            }
        },
        hide() {
			this.flag = false; 
        },
        // 确认购买
        confirm() {
            var sku = this.sku;
            var Fstocknum = Number(sku.Ftotal) - Number(sku.Fsalenum);
            if (Number(this.Fnum) > Fstocknum) {
                uni.showModal({
                    title: '提示',
                    content: '库存仅剩余=' + Fstocknum
                });
                return;
            }
            var Fnum = this.Fnum;
            if (Number(Fnum) > Number(sku.Flimitnum) && Number(sku.Flimitnum) > 0) {
                uni.showModal({
                    title: '提示',
                    content: '活动每人限购' + sku.Flimitnum + '件'
                });
                return;
            }
            var param = {
                Fopt: this.Fopt,
                Fnum: Fnum
            };
            this.$emit('config', {
                detail: param
            });
        }
    },
    created: function () {}
};
</script>
<style>
@import './sqladialog.css';
</style>
